﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Net;


/// <summary>
/// Summary description for PaymentManager
/// </summary>
public static class PaymentManager
{
    public static bool RequestPayment(string Id, bool Accept )
    { // true potvrdit, false zamitnout
        Payment pay = DatabaseManager.getPayment(Id);
        

        if (pay != null)
        {
            Account accountFrom = DatabaseManager.getAccount(pay.FromAccount);
            Account accoutWhom = DatabaseManager.getAccount(pay.Whom);
            Double amount = Convert.ToDouble(pay.Amount);

            if (Accept == true)
            {
                if (amount > Convert.ToDouble(accountFrom.Balance))
                {
                    return false;
                }
                else
                {
                    pay.Status = Configuration.PaymentStatus.paid.ToString("g");

                    accountFrom.Balance = Convert.ToString(Convert.ToDouble(accountFrom.Balance) - amount);
                    accountFrom.OutcomesCount = Convert.ToString(Convert.ToInt32(accountFrom.OutcomesCount)+1);
                    accoutWhom.Balance = Convert.ToString(Convert.ToDouble(accoutWhom.Balance) + amount); ;
                    accoutWhom.IncomesCount = Convert.ToString(Convert.ToInt32(accoutWhom.IncomesCount) + 1);

                    pay.Amount = ((int)Convert.ToDouble(pay.Amount)).ToString();
                    DatabaseManager.editAccount(accountFrom.Id, ref accountFrom);
                    DatabaseManager.editAccount(accoutWhom.Id, ref accoutWhom);
                    DatabaseManager.editPayment(Id, ref pay);


                    // Zaslat odpoved eShopu o přijeti platby
                    //WebRequest request = WebRequest.Create("pay.Guid");
                    //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                }
            }
            else
            {
                pay.Status = Configuration.PaymentStatus.refused.ToString("g");
                DatabaseManager.editPayment(Id, ref pay);
            }
            
            return true;
        }
        else
        {
            return false;
        }
    }
    public static bool payOnline(string fromUser, string toUser, string amount, string comment)
    {
        Person person;
        Payment pay;
        Account From, To;
        string accountFrom = "", accountTo = "";

        person = DatabaseManager.getPerson(fromUser);
        if (person != null)  accountFrom = person.Account;
        person = DatabaseManager.getPerson(toUser);
        if (person != null) accountTo = person.Account;

        if ((accountFrom == "") || (accountTo == "")) return false;

        From = DatabaseManager.getAccount(accountFrom);
        To = DatabaseManager.getAccount(accountTo);

        if (Convert.ToDouble(amount) > Convert.ToDouble(From.Balance)) return false; // nedostatek penez

        pay = new Payment("0", "", accountFrom, DateTime.Now.ToString(), DateTime.Now.ToString(), DateTime.Now.ToString(), "paid", "online", amount, accountTo, comment);

        From.Balance = (Convert.ToDouble(From.Balance) - Convert.ToDouble(amount)).ToString();
        From.OutcomesCount = Convert.ToString(Convert.ToInt32(From.OutcomesCount) + 1);
        To.Balance = (Convert.ToDouble(To.Balance) + Convert.ToDouble(amount)).ToString();
        To.IncomesCount = Convert.ToString(Convert.ToInt32(To.IncomesCount) + 1);

        DatabaseManager.editAccount(From.Id, ref From);
        DatabaseManager.editAccount(To.Id, ref To);
        DatabaseManager.savePayment(ref pay);

        return true;
    }

    public static Guid saveCheck(string fromUser, string toUser, string amount, string date_Splatnost, string comment)
    {
        Person person;
        Payment pay;
        Account From, To;
        string accountFrom = "", accountTo = "";

        Guid guid = Guid.NewGuid();

        person = DatabaseManager.getPerson(fromUser);
        if (person != null) accountFrom = person.Account;
        person = DatabaseManager.getPerson(toUser);
        if (person != null) accountTo = person.Account;

        if (accountFrom == "") return Guid.Empty;

        From = DatabaseManager.getAccount(accountFrom);
        if (accountTo != "")
            To = DatabaseManager.getAccount(accountTo);


        pay = new Payment("0", guid.ToString(), accountFrom, DateTime.Now.ToString(), /*date_Splatnost*/ DateTime.Now.ToString(), DateTime.Now.ToString(), Configuration.PaymentStatus.created.ToString("g"), Configuration.PaymentType.check.ToString("g"), amount, accountTo, comment);
        pay.Amount = ((int)Convert.ToDouble(pay.Amount)).ToString();
        DatabaseManager.savePayment(ref pay);

        return guid;
    }

    public static bool Check(string toUser, string guid_str, bool onlyVerify)
    {
        Person person;
        person = DatabaseManager.getPerson(toUser);
        Guid guid = new Guid(guid_str);
        Account To, From;
        if (person != null)
        {
            To = DatabaseManager.getAccount(person.Account);
        }
        else
        {

            return false;
        }
        Payment pay = DatabaseManager.getPaymentByGUID(guid);
        if (pay == null) return false;
        if (pay.Status != Configuration.PaymentStatus.created.ToString("g")) return false;

        From = DatabaseManager.getAccount(pay.FromAccount);

        if ((pay.Whom != "") && (pay.Whom != To.Id.ToString())) return false;

        if (Convert.ToDouble(pay.Amount) > Convert.ToDouble(From.Balance)) return false;

        // v tuto chvili je možne nechat šek proplatit
        if (onlyVerify == false)
        {
            pay.Status = Configuration.PaymentStatus.paid.ToString("g");
            From.Balance = (Convert.ToDouble(From.Balance) - Convert.ToDouble(pay.Amount)).ToString();
            From.OutcomesCount = Convert.ToString(Convert.ToInt32(From.OutcomesCount) + 1);
            To.Balance = (Convert.ToDouble(To.Balance) + Convert.ToDouble(pay.Amount)).ToString();
            To.IncomesCount = Convert.ToString(Convert.ToInt32(To.IncomesCount) + 1);

            pay.Amount = ((int)Convert.ToDouble(pay.Amount)).ToString();

            DatabaseManager.editAccount(From.Id, ref From);
            DatabaseManager.editAccount(To.Id, ref To);
            DatabaseManager.editPayment(pay.Id, ref pay);
        }
        return true;

    }

    public static void Timer()
    {
        

    }
	
}
